package com.hsl.housaileibot001.controller;

import com.hsl.housaileibot001.common.PageRequest;
import com.hsl.housaileibot001.common.PageResult;
import com.hsl.housaileibot001.common.Result;
import com.hsl.housaileibot001.model.dto.PracticeRecordDTO;
import com.hsl.housaileibot001.model.vo.PracticeRecordVO;
import com.hsl.housaileibot001.service.PracticeRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 答题记录控制器
 */
@Tag(name = "答题记录接口", description = "答题记录相关接口")
@RestController
@RequestMapping("/practice")
public class PracticeRecordController {

    @Resource
    private PracticeRecordService practiceRecordService;

    @Operation(summary = "提交答题记录", description = "提交用户的答题记录")
    @PostMapping("/record")
    public Result<Void> submitRecord(@Valid @RequestBody PracticeRecordDTO recordDTO) {
        try {
            practiceRecordService.submitRecord(recordDTO);
            return Result.success();
        } catch (Exception e) {
            return Result.error("提交答题记录失败：" + e.getMessage());
        }
    }

    @Operation(summary = "获取答题记录列表", description = "分页获取当前用户的答题记录")
    @GetMapping("/record")
    public Result<PageResult<PracticeRecordVO>> getRecords(@ModelAttribute PageRequest pageRequest) {
        try {
            PageResult<PracticeRecordVO> result = practiceRecordService.getRecords(pageRequest);
            return Result.success(result);
        } catch (Exception e) {
            return Result.error("获取答题记录失败：" + e.getMessage());
        }
    }

    @Operation(summary = "获取答题统计信息", description = "获取当前用户的答题统计信息")
    @GetMapping("/record/stats")
    public Result<Map<String, Object>> getStats() {
        try {
            Map<String, Object> stats = practiceRecordService.getStats();
            return Result.success(stats);
        } catch (Exception e) {
            return Result.error("获取统计信息失败：" + e.getMessage());
        }
    }
}

